var express = require('express')
var connection = require('../db/db_utill');

var router = express.Router()

//获取随机话题
router.post('/random_topic', function (req, res) {
  const body = req.body

  connection.query(`SELECT * FROM topic_info ORDER BY RAND() LIMIT ?`, [body.topic_num], function (error, results, fields) {
    if (error) {
      throw error;
    } else if (results.length) {
      var string = JSON.stringify(results);
      var topic_info = JSON.parse(string);
      res.json({
        status: 200,
        //话题数据
        topic_info
      })
    } else {
      res.json({
        message: '获取数据失败！'
      })
    }
  });
})


//获取话题信息
router.post('/topic_content', function (req, res) {
  //获取话题内容
  const topicId = req.body.topicId
  connection.query(`select * from topic_info where topic_id = ?`, [topicId], function (error, results, fields) {
    if (error) {
      throw error;
    } else {
      const string = JSON.stringify(results);
      const jsons = JSON.parse(string)[0];
      return res.json(jsons)
    }
  });
})


module.exports = router;